//
//  TGTabBarVC.m
//  TongGuanLease
//
//  Created by 张奇 on 2022/4/12.
//

#import "TGTabBarVC.h"
#import "TGCustomAddTabBar.h"
#import "TGBaseNavigationVC.h"

//#import "TGBulletinBoardVC.h"

@interface TGTabBarVC ()<UITabBarControllerDelegate>

@end

@implementation TGTabBarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addChildViewControllers];
    [self configureTabBar];
    self.delegate = self;
}

- (void)configureTabBar {
    [[UITabBar appearance] setUnselectedItemTintColor:RGB(78, 80, 92)];
    [[UITabBar appearance] setTintColor:GlobalBarTinColor];
}

- (void)addChildViewControllers {

    if ([[TGUserModel getUserEntity].authorities containsObject:@"app_event_center"]) {
        [self addChildViewController:@"EventCenterVC" title:@"事件中心" imageName:@"tabbar_eventCenter"];
    }
    if ([[TGUserModel getUserEntity].authorities containsObject:@"app_event_statistics"]) {
        [self addChildViewController:@"EventStatisticsVC" title:@"事件统计" imageName:@"tabbar_eventStatistics"];
    }
    if ([[TGUserModel getUserEntity].authorities containsObject:@"app_clap"]) {
        [self addChildViewController:@"ConvenientlyTakeVC" title:@"随手拍" imageName:@"tabbar_convenientlyTake"];
    }
    [self addChildViewController:@"MineVC" title:@"我的" imageName:@"tabbar_mine"];
    
}

#pragma mark 添加子视图控制器详细方法
- (void)addChildViewController:(NSString *)childVCString title:(NSString *)title imageName:(NSString *)icon
{
    Class childClass = NSClassFromString(childVCString);
    UIViewController *childController = [[childClass alloc] init];
    UITabBarItem *item = childController.tabBarItem;
    
    UIImage *iconImage = [[UIImage imageNamed:icon] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    NSString *selectImageName = [icon stringByAppendingString:@"_selected"];
    UIImage *selectIconImage = [[UIImage imageNamed:selectImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    item.image = iconImage;
    item.selectedImage = selectIconImage;
    item.title = title;
    childController.title = title;
    
    TGBaseNavigationVC *nav = [[TGBaseNavigationVC alloc]initWithRootViewController:childController];
    [self addChildViewController:nav];
}

#pragma mark - UITabBarControllerDelegate
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{

    
    // 判断哪个界面要需要再次点击刷新
//    if ([tabBarController.selectedViewController isEqual:[tabBarController.viewControllers firstObject]]) {
//        // 判断再次选中的是否为当前的控制器
//        if ([viewController isEqual:tabBarController.selectedViewController]) {
//            // 执行操作
//            NSLog(@"刷新界面");
//            NSLog(@"viewController===%@",viewController);
//            TGBaseNavigationVC *nav = (TGBaseNavigationVC *)viewController;
//            UIViewController *selectedVC = [nav.viewControllers firstObject];
//            if ([selectedVC isKindOfClass:[TGBulletinBoardVC class]])
//            {
//                TGBulletinBoardVC *vc = (TGBulletinBoardVC *)selectedVC;
//                [vc bulletinBoardVCRefreshData];
//                return NO;
//            }
//
//        }
//
//    }
    
        
//    BaseNavigationViewController *nav = (BaseNavigationViewController *)viewController;
//    UIViewController *selectedVc = [nav.viewControllers firstObject];
//    if ([selectedVc isKindOfClass:[MineViewController class]] || [selectedVc isKindOfClass:[CartViewController class]]) {
//        if (!kUserLogin) {
//            [ShowLoginManager showLoginVcFrom:self];
//            return NO;
//        }
//    }
    return YES;
}

@end
